<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>学生信息管理</title>
  <link rel="stylesheet" href="css/index.css" />
</head>

<body>
  <h1>新增学员</h1>
  <form class="info" autocomplete="off">
    姓名：<input type="text" class="uname" name="uname" />
    年龄：<input type="text" class="age" name="age" />
    性别:
    <select name="gender" class="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资：<input type="text" class="salary" name="salary" />
    就业城市：<select name="city" class="city">
      <option value="北京">北京</option>
      <option value="上海">上海</option>
      <option value="广州">广州</option>
      <option value="深圳">深圳</option>
      <option value="曹县">曹县</option>
    </select>
    <button class="add">录入</button>
  </form>

  <h1>就业榜</h1>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <!-- 
        <tr>
          <td>1001</td>
          <td>欧阳霸天</td>
          <td>19</td>
          <td>男</td>
          <td>15000</td>
          <td>上海</td>
          <td>
            <a href="javascript:">删除</a>
          </td>
        </tr> 
        -->
    </tbody>
  </table>
  <script>
  // 参考数据
      // const initData = [
        // {
        //   stuId: 1001,
        //   uname: '欧阳霸天',
        //   age: 19,
        //   gender: '男',
        //   salary: '20000',
        //   city: '上海',
        // }
      // ]
      
      const tbody = document.querySelector('tbody');
      const form = document.querySelector('form');
      const renderData = JSON.parse(localStorage.getItem('studentInfo'))||[]
      // 封装渲染函数
      function render(data) {
        const renderlist=data.map(function(item,index){
          let str=`<tr>
          <td>${item.stuId}</td>
          <td>${item.uname}</td>
          <td>${item.age}</td>
          <td>${item.gender}</td>
          <td>${item.salary}</td>
          <td>${item.city}</td>
          <td>
            <a href="javascript:" data-id=${index}>删除</a>
          </td>
        </tr>`
        return str
        })
        tbody.innerHTML=renderlist.join('')
      }
      render(renderData)
      // 信息录入
      const list = form.querySelectorAll('[name]');
      form.addEventListener('submit',function(e){
        e.preventDefault()
        for(let i = 0;i<list.length;i++){
          if(list[i].value==='') {
            return alert('请输入必填项')
          }
        }
        // const id = renderData.length===0?1:renderData[renderData.length-1].stuId+1
        const obj={
          // 可选链式
          stuId: renderData[renderData.length-1]?.stuId+1||1,
          uname: list[0].value,
          age: list[1].value,
          gender: list[2].value,
          salary: list[3].value,
          city: list[4].value,
        }
        renderData.push(obj)
        localStorage.setItem('studentInfo',JSON.stringify(renderData))
        render(renderData)
        this.reset()
      })
      // 信息删除
      tbody.addEventListener('click',function(e){
        if(e.target.tagName==='A'){
          renderData.splice(e.target.dataset.id,1)
          localStorage.setItem('studentInfo',JSON.stringify(renderData))
        }
        render(renderData)
      })
  </script>
</body>
</html>